<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" %>
<%@ page 
	import="java.util.*"
	import="net.sf.json.*"
	import="com.markan.facebook.*"
	import="com.markan.facebook.utl.*" %>    
<%
	final String NBSP = "&nbsp";

	//액세스 토큰
	//String accessToken = (String) session.getAttribute("access_token");	

	//FB 객체 생성
	//Facebook facebook = new Facebook(accessToken);
	
	String APP_ID = Facebook.getKeyString_inProperties("APP_ID");
	Facebook facebook = new Facebook(APP_ID, request);
	String accessToken = facebook.getAccessToken();
	
	//id
	String id = request.getParameter("id");
	
	//그룹 정보를 가져온다.
	JSONObject jsonObj = facebook.getUserInfo(id);
	int version = MknJSONUtil.getObject2Integer(jsonObj.get("version"));
	String name = (String) jsonObj.get("name");
	String privacy = (String) jsonObj.get("privacy");
	String icon = (String) jsonObj.get("icon");
	String updated_time = (String) jsonObj.get("updated_time");
	String email = (String) jsonObj.get("email");
	
	JSONObject ownerJsonObj = MknJSONUtil.getObject2JSONObject(jsonObj.get("owner"));
	String owner_name = (String) ownerJsonObj.get("name");
	String owner_id = (String) ownerJsonObj.get("id"); 
	
	//
	Map<String, String> privacyKorMap = new HashMap<String, String>();
	privacyKorMap.put("SECRET", "비밀");
	privacyKorMap.put("CLOSED", "비공개");
	privacyKorMap.put("OPEN", "공개");
%>	
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" href="http://static.jquery.com/files/rocker/css/reset.css" type="text/css" />
<!-- <link rel="stylesheet" href="http://static.jquery.com/files/rocker/css/screen.css" type="text/css" /> -->
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>!window.jQuery && document.write('<script src="http://code.jquery.com/jquery-1.4.2.min.js"><\/script>');</script>
<title><%=name %></title>
<style>
body { font-size:10pt;font-family:'맑은 고딕'}
/*a:link { text-decoration: none; }*/
.listView ul	{list-style:none; padding:0; margin:0;}
.listView ul li	{list-style:none; border-bottom:1px solid #bbb;}
/*.listView ul li:hover	{background-color:#e5e5e5;}*/
.listView li div.name { font-weight:bold;font-family:"Times New Roman",Georgia,Serif; }
dl {
    border: 3px double #ccc;
    padding: 0.5em;
  }
dt {
  float: left;
  clear: left;
  width: 100px;
  text-align: right;
  font-weight: bold;
  color: green;
}
dt:after {
  content: ":";
}
dd {
  margin: 0 0 0 110px;
  padding: 0 0 0.5em 0;
}

.clearfix::after {
clear: both;
content: ".";
display: block;
font-size: 0;
height: 0;
line-height: 0;
visibility: hidden;
}

.uiStreamFooter, .uiStreamSource, .uiStreamAdditionalLogging {
	color: #999;
}
.uiStreamFooter {margin-top:10px}

div.more { height:105px;overflow:hidden; }
div.height_auto {height:auto}

.listView { margin:10px}

/* comments */
ul.commentList {
	margin: 0 35px 14px 10px;
	padding-top: 14px;
}
li.ufiItem {
	background-color: #EDEFF4;
	margin-top: 1px;
	padding: 5px 5px 4px;
}
</style>
<script>
$(document).ready(function() {
	$('.moreBtn').click(function() {
		var feed_id = $(this).attr("feed");
		var feed_div = $('#feed_' + feed_id);
		
		feed_div.toggleClass( "height_auto" );
		
/* 		if(feed_div.hasClass("height_auto"))
			feed_div.
		else */
		
		//return false;
	});
});	
</script>
</head>
<body>
<!-- <h1>그룹 상세</h1>
<h2>그룹 정보</h2> -->
<div>
<dl>
<dt>그룹명</dt><dd><img src="<%=icon %>"/> <%=name %></dd>
<dt>공개여부</dt><dd><%=privacyKorMap.get(privacy) %></dd>

<dt>그룹메일</dt><dd><a href="mailto:<%=email %>"><%=email %></a></dd>
<dt>관리자</dt><dd><a href="userDeatil.jsp?id=<%=owner_id %>"><%=owner_name %></a></dd>
</dl>
</div>

<div class="writeForm" style="display:none" onclick="if(this.style.disply='none'){this.style.disply=''}else{this.style.disply='none'}">
	<!-- message, picture, link, name, caption, description, source, place, tags -->
	<form name="f" id="writeForm"  method="post" action="https://graph.facebook.com/<%=id%>/feed" onsubmit="return formValid(this)">
	<fieldset>
		<legend>글쓰기</legend>
		<input type="hidden" name="access_token" value="<%=accessToken%>" /><br>
		<label for="message">message</label> <textarea id="message" name="message" cols="50" rows="5" autocomplete="off"></textarea><br>
		<label for="picture">picture</label> <input type="text" id="picture" name="picture" value="" />(URL)<br>
		<label for="link">link</label> <input type="text" id="link" name="link" value="" />(링크 URL)<br>
		<label for="name">name</label> <input type="text" id="name" name="name" value="" />(링크명)<br>
		<label for="caption">caption</label> <input type="text" id="caption" name="caption" value="" autocomplete="off"/>(링크요약)<br>
		<label for="description">description</label> <input type="text" id="description" name="description" value="" autocomplete="off"/>(링크설명)<br>
		<label for="source">source</label> <input type="text" id="source" name="source" value="" />(URL)<br>
		<label for="place">place</label> <input type="text" id="place" name="place" value="" />(Param place must be a valid page ID)<br>
		<label for="tags">tags</label> <input type="tags" id="tags" name="tags" value="" />(Cannot specify user tags without a place tag)<br>
		<!-- <input type="submit" />  -->
		<a href="javascript:send()">전송</a>
	</fieldset>
	</form>
	<script>
	function send() {
		try{
			//$.post("https://graph.facebook.com/<%=id%>/feed", $("#writeForm").serialize());
			$.ajax({
				type: 'POST',
				url: "https://graph.facebook.com/<%=id%>/feed",
				data: $("#writeForm").serialize(),
				dataType: "text", 
				success: function(data) {
					var jsonObj =  eval("(" + data + ")");
					alert(jsonObj.id);
				},
				error: function() { 
					alert("에러발생"); 
				}
			});
		}catch(err){alert(err.message)}
	}
	</script>
</div>

<!-- <h2>뉴스 피드</h2> -->
<div class="listView">
<%
	JSONObject feedjsonObj = facebook.getProfileFeed(id);

	out.println("<ul>");
	
	JSONArray feedDataJSONArray = MknJSONUtil.getObject2JSONArray(feedjsonObj.get("data"));
	for(int i=0, feedSize = feedDataJSONArray.size(); i<feedSize; i++ ) {
		
		JSONObject feedDataJsonObj = (JSONObject) feedDataJSONArray.get(i);

		String feedId = (String) feedDataJsonObj.get("id");
		String feedMessage = (String) feedDataJsonObj.get("message");
		String feedPicture = (String) feedDataJsonObj.get("picture");
		String feedLink = (String) feedDataJsonObj.get("link");
		String feedIcon = (String) feedDataJsonObj.get("icon");
		String feedObjectId = (String) feedDataJsonObj.get("object_id");
		String feedType = (String) feedDataJsonObj.get("type");	//status
		String feedCreatedTime = (String) feedDataJsonObj.get("created_time");	
		String feedUpdatedTime = (String) feedDataJsonObj.get("updated_time");	
		
		//from
		JSONObject fromJsonObj = MknJSONUtil.getObject2JSONObject(feedDataJsonObj.get("from"));
		String fromId = (String) fromJsonObj.get("id");
		String fromName = (String) fromJsonObj.get("name");
		
		//to
		JSONObject toJsonObj = MknJSONUtil.getObject2JSONObject(feedDataJsonObj.get("to"));
		JSONArray toJSONArray = MknJSONUtil.getObject2JSONArray(toJsonObj.get("data"));
		for(int j=0, toSize = toJSONArray.size(); j<toSize; j++ ) {
			JSONObject toDataJsonObj = (JSONObject) toJSONArray.get(j);
			
			int toVersion = MknJSONUtil.getObject2Integer(toDataJsonObj.get("version"));
			String toId = (String) toDataJsonObj.get("id");
			String toName = (String) toDataJsonObj.get("name");
		}
//		String aLinkUrl = "https://graph.facebook.com/"+feedId+"?access_token='"+accessToken;
		String aLinkUrl = "http://www.facebook.com/groups/" + feedId.replace("_", "/") + "/";
		
//------ print ----------------------------------------------------------------
		out.println("	<li>");
		out.println("		<div class='UIImageBlock clearfix'>");
		out.println("			<div class='name'>" + fromName + "</div>");
		
		int maxMessageLength = 500;
		if(feedMessage.length() < maxMessageLength) {
			out.println("		<div class='message' id='feed_"+feedId+"'>");	
		} else {
			out.println("		<div class='message more' id='feed_"+feedId+"'>");	
		}
		out.println( 				feedMessage.replaceAll("\n", "<br>") );	//메세지
		out.println("			</div>");
		if(feedMessage.length() > maxMessageLength) {
			out.println("<a href='#feed_"+feedId+"' class='moreBtn' feed="+feedId+">더보기</a>");	
		} 
		out.println("			<div>");
		out.println( 				htmlALink(feedLink, htmlImg(feedPicture), "_blank") );		//이미지
		out.println("			</div>");
		out.println("			<div class='UIImageBlock clearfix uiStreamFooter'>");
//		out.println( 				htmlImg(feedIcon));
		out.println("				·");
		out.println( 				feedCreatedTime.replaceAll("T", " ").replaceAll("\\+0000", ""));
		out.println("				·");
		out.println("				<a href='"+aLinkUrl+"' target='_blank'>페이스북으로 이동</a>");
		out.println("			</div>");
//------ print ----------------------------------------------------------------
		
		//댓글 영역
		out.println("			<ul class='commentList'>");
		//comments
		JSONObject commentsJsonObj = MknJSONUtil.getObject2JSONObject(feedDataJsonObj.get("comments"));
		int commentsCount = MknJSONUtil.getObject2Integer(commentsJsonObj.get("count"));
		JSONArray commentsJsonArray = MknJSONUtil.getObject2JSONArray(commentsJsonObj.get("data"));
		for(int j=0, commentsSize = commentsJsonArray.size(); j<commentsSize; j++ ) {
			JSONObject commentDataJsonObj = MknJSONUtil.getObject2JSONObject(commentsJsonArray.get(j));
			
			String commentId = (String) commentDataJsonObj.get("id");
			String commentMessage = (String) commentDataJsonObj.get("message");
			String commentCreatedTime = (String) commentDataJsonObj.get("created_time");
			
			//comment from
			JSONObject commentFromJsonObj = MknJSONUtil.getObject2JSONObject(commentDataJsonObj.get("from"));
			String commentFromId = (String) commentFromJsonObj.get("id");
			String commentFromName = (String) commentFromJsonObj.get("name");
			
			//------ print ----------------------------------------------------------------
			out.println("				<li class='uiUfiComment comment_"+commentId+" ufiItem ufiItem'>");
			out.println("					<div class='UIImageBlock clearfix uiUfiActorBlock'>");
			out.println("					<div class='name'>" + commentFromName + "</div>");
			out.println("					<div class='message'>" + commentMessage + "</div>");
			out.println( 						commentCreatedTime.replaceAll("T", " ").replaceAll("\\+0000", ""));
			out.println("					</div>");
			out.println("				</li>");
			//------ print ----------------------------------------------------------------
		}
		out.println("			</ul>");
		
		//action
		/*
		JSONArray actionJSONArray = MknJSONUtil.getObject2JSONArray(feedDataJsonObj.get("actions"));
		for(int j=0, actionSize = actionJSONArray.size(); j<actionSize; j++ ) {
			JSONObject actionDataJsonObj = (JSONObject) actionJSONArray.get(j);
			
			String actionName = (String) actionDataJsonObj.get("name");
			String actionLink = (String) actionDataJsonObj.get("link");
			
			out.println("<a href='" + actionLink + "'>" + actionName + "</a>");
		}
		*/
		
		out.println("		</div>");
		out.println("	</li>");
		
	} //for feedDataJSONArray
	
	out.println("</ul>");
	
	//Paging
	JSONObject feedPagingJsonObj = MknJSONUtil.getObject2JSONObject(feedjsonObj.get("paging"));
	String feedPagingPrevios = (String) feedPagingJsonObj.get("previous");
	String feedPagingNext = (String) feedPagingJsonObj.get("next");

%>
</div>

<%if(request.getParameter("debug")!=null) { %>
<iframe src="<%=facebook.GRAPH_URL+id+"/feed?access_token="+accessToken%>" style="width:100%;height:500px"></iframe>
<% } %>
<!-- 
{"data":[{"id":"186577728129797_187056391415264","from":{"name":"Jeong Seok Oh","id":"100001115586163"},"to":{"data":[{"version":1,"name":"도라에몽","id":"186577728129797"}]},"message":"도라에몽 프로젝트를 시작합니다.","actions":[{"name":"Comment","link":"http://www.facebook.com/186577728129797/posts/187056391415264"},{"name":"Like","link":"http://www.facebook.com/186577728129797/posts/187056391415264"}],"type":"status","created_time":"2012-04-20T02:55:53+0000","updated_time":"2012-04-20T02:55:53+0000","comments":{"count":0}}],"paging":{"previous":"https://graph.facebook.com/186577728129797/feed?access_token=AAACw2R6RusIBAN2t93O6B1PJopctfvjGGsl3szRBjxkx6rWvpdlvMnPZAqx7gbdkQrBjxGpYpwESazLCfmZB0mA52eKq4l7q798uRRnxDTOWcZAKyr2&limit=25&since=1334890553&__paging_token=186577728129797_187056391415264&__previous=1","next":"https://graph.facebook.com/186577728129797/feed?access_token=AAACw2R6RusIBAN2t93O6B1PJopctfvjGGsl3szRBjxkx6rWvpdlvMnPZAqx7gbdkQrBjxGpYpwESazLCfmZB0mA52eKq4l7q798uRRnxDTOWcZAKyr2&limit=25&until=1334890553&__paging_token=186577728129797_187056391415264"}}
 -->
</body>
</html>
<%!
	public String htmlImg(String src) {
		if(src==null)
			return "";
		return "<img src='" + src + "'>";
	}
	public String htmlALink(String href, String name, String target) {
		if(href==null || name==null || name.isEmpty())
			return "";
		return "<a href='" + href + "' target='"+ target +"'>" +name + "</a>";
	}
%>

